package org.feuyeux.mysql.hello.domain;

import java.util.HashMap;
import java.util.Map;
import lombok.Data;

@Data

public class UserResult<T> {
  //成功状态码
  public static final int OK = 20000;
  //失败状态码
  public static final int ERROR = 0;
  //返回码
  private Integer code;
  //返回消息
  private String message;
  //存放数据
  private T data;
  //其他数据
  private Map<String, Object> other = new HashMap<>();

  public UserResult() {
  }

  public UserResult(Integer code, String message) {
    this.code = code;
    this.message = message;
  }

  public UserResult(Integer code, String message, T data) {
    this.code = code;
    this.message = message;
    this.data = data;
  }

  /**
   * 快捷成功BaseResult对象
   *
   * @param message
   * @return
   */
  public static UserResult ok(String message) {
    return new UserResult<>(UserResult.OK, message);
  }

  public static <T> UserResult ok(String message, T data) {
    return new UserResult<>(UserResult.OK, message, data);
  }

  /**
   * 快捷失败BaseResult对象
   *
   * @param message
   * @return
   */
  public static UserResult error(String message) {
    return new UserResult(UserResult.ERROR, message);
  }

  /**
   * 自定义数据区域
   *
   * @param key
   * @param msg
   * @return
   */
  public UserResult append(String key, T msg) {
    other.put(key, msg);
    return this;
  }

  public Integer getCode() {
    return code;
  }

  public String getMessage() {
    return message;
  }

  public T getData() {
    return data;
  }

  public Map<String, Object> getOther() {
    return other;
  }
}
